<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>

<script language="javascript">

function open_popup(){
	

	if(document.formsearch.monthyear.checked == false && document.formsearch.everyyear.checked == false  )
	{
		alert('กรุณาเลือกช่วงเวลา/รายการ');
		return false;
	}
	
	if(document.formsearch.monthyear.checked == true && document.formsearch.year.value == "" )
	{
		alert('กรุณาเลือกปี');
		document.formsearch.year.focus();
		return false;
	}
	document.formsearch.submit();
}
</script></head>
<body>
<?php
include("../connect/connect.php");

//	$Query = "SELECT * FROM sale ORDER BY date desc ";

if($_POST["timelist"]!=""){

if($_POST["timelist"]=='monthyear'){
		$Query = "select t1.month,t1.amount,t1.cost,t2.expend from (select SUM(amount) as amount, sum(cost) as cost, month(date) as month from sale WHERE year(date)='".$_POST["year"]."' group by month(date)) as t1 left join (select SUM(amount) as expend, month(date) as date from expend group by month(date)) as t2 on t1.month=t2.date order by t1.month,t2.date";
}

if($_POST["timelist"]=='everyyear'){
//	$Query = "SELECT year(date),SUM(amount) FROM sale GROUP BY year(date)";
	$Query = "select t1.year1,t1.amount,t1.cost,t2.expend from (select SUM(amount) as amount, sum(cost) as cost, year(date) as year1 from sale group by year (date)) as t1 left join (select SUM(amount) as expend, year(date) as year2 from expend group by year(date)) as t2 on t1.year1=t2.year2 ORDER BY t1.year1,t2.year2";

}

	$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
}
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>รายงานสรุปผลกำไร-ขาดทุน ตามเดือน และปี</h1>
    </div>
  <div class="content">
  <form action="<?php echo $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
    <table class="list2" width="450" border="0" align="center" cellpadding="5" cellspacing="0">
      <thead>
        <tr>
          <td width="130" align="right">&nbsp;</td>
          <td width="110">&nbsp;</td>
          <td width="178">&nbsp;</td>
        </tr>
      </thead>
      <tr>
        <td align="right">เลือกช่วงเวลา / รายการ : </td>
        <td><input name="timelist" type="radio" value="monthyear" id="monthyear" <?php if($_POST["timelist"]=='monthyear'){echo "checked";} ?> />
            <label for="monthyear">รายเดือนตามปี</label></td>
        <td><select name="year" id="year">
            <option value="">กรุณาเลือกปี</option>
			<?php
			$Queryyear = "SELECT year(date) FROM sale GROUP BY year(date) ORDER BY date DESC";
			$objQueryyear = mysql_query($Queryyear);
			while($row = mysql_fetch_assoc($objQueryyear))
			{
			?>
                <option value="<?php echo $row["year(date)"];?>" <?php if($_POST["timelist"]=='monthyear' && $_POST["year"]==$row["year(date)"]) echo "selected = 'selected'";?> >
                <?php echo $row["year(date)"]+543;?>                </option>
            <?php
			}
			?>
          </select>        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input name="timelist" type="radio" value="everyyear" id="everyyear" <?php if($_POST["timelist"]=='everyyear'){echo "checked";} ?> />
            <label for="everyyear">ทุกปี</label></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="3" align="center"><div class="buttons"><a onclick="open_popup();" class="button"><span>แสดงรายงาน</span></a></div>           </td>
      </tr>
    </table>
    <br />
  </form>
  <?php
 if($_POST["timelist"]=='monthyear'){
 ?>
    <div class="heading2"><?php $Queryshop = mysql_query("SELECT shop_name FROM shopname WHERE shop_id='1'"); $Shop = mysql_fetch_assoc($Queryshop); echo $Shop["shop_name"]?></div>
	<div class="heading2">รายงานสรุปผลกำไร-ขาดทุน</div>
	<div class="heading2">
<?php 
	$y = $_POST["year"]+543;
	echo "ประจำปี พ.ศ. ".$y;
?></div>
    <table class="listshow">
      <thead>
        <tr>
          <td width="9%" style="text-align: center;"><strong>ลำดับ</strong></td>
          <td width="26%" class="center"><strong>เดือน</strong></td>
          <td width="16%" class="center"><strong>รายรับ (บาท)</strong></td>
          <td width="15%" class="center"><strong>ต้นทุน (บาท)</strong></td>
          <td width="15%" class="center"><strong>รายจ่าย (บาท)</strong></td>
          <td width="19%" class="center"><strong>กำไร-ขาดทุน (บาท) </strong></td>
        </tr>
      </thead>
      <tbody>
        <?php
	$strnum=0;
	$strAmount=0;
	$strCost=0;
	$strExpend=0;
	while($row = mysql_fetch_array($objQuery))
		{
		$strnum++;
?>
        <tr>
          <td style="text-align: center;"><?php echo $strnum;?></td>
          <td class="center">
<?php 
		  $monthname = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
		  $month = $row["month"];
		  echo $monthname[$month-1];
?></td>
          <td class="center"><?php echo number_format($row["amount"], 2,'.',',');?></td>
          <td class="center"><?php echo number_format($row["cost"], 2,'.',',');?></td>
          <td class="center"><?php echo number_format($row["expend"], 2,'.',',');?></td>
          <td class="center"><?php
		  $amount = $row["amount"];
		  $cost = $row["cost"];
		  $expend = $row["expend"];
		  $profit = ($amount-$cost)-$expend;
		   echo number_format($profit, 2,'.',',');?></td>
        </tr>
        <?php
		$strTotal=$strTotal+$profit;
		$strAmount=$strAmount+($row["amount"]);
		$strCost=$strCost+($row["cost"]);
		$strExpend=$strExpend+($row["expend"]);
		}

	if($strnum>0) {
		?>
        <tr>
          <td colspan="2" style="text-align: center;"><strong>รวมสุทธิ (บาท) </strong></td>
          <td class="center"><strong><?php echo number_format($strAmount, 2,'.',',');?></strong></td>
          <td class="center"><strong><?php echo number_format($strCost, 2,'.',',');?></strong></td>
          <td class="center"><strong><?php echo number_format($strExpend, 2,'.',',');?></strong></td>
          <td class="center"><strong><?php echo number_format($strTotal, 2,'.',',');?></strong></td>
        </tr>
        <?php
		}
		if($strnum==0) {
?>
        <tr>
          <td colspan="9" class="center"><strong>ไม่มีรายการ</strong></td>
        </tr>
        <?php
		}
?>
      </tbody>
    </table>
<?php
}  

if($_POST["timelist"]=='everyyear') { 
?>
    <div class="heading2"><?php $Queryshop = mysql_query("SELECT shop_name FROM shopname WHERE shop_id='1'"); $Shop = mysql_fetch_assoc($Queryshop); echo $Shop["shop_name"]?></div>
	<div class="heading2">รายงานสรุปผลกำไร-ขาดทุน</div>
	<div class="heading2">ของทุกปี</div>
    <table class="listshow">
      <thead>
        <tr>
          <td width="9%" style="text-align: center;"><strong>ลำดับ</strong></td>
          <td width="26%" class="center"><strong>ปี</strong></td>
          <td width="16%" class="center"><strong>รายรับ (บาท)</strong></td>
          <td width="15%" class="center"><strong>ต้นทุน (บาท)</strong></td>
          <td width="15%" class="center"><strong>รายจ่าย (บาท)</strong></td>
          <td width="19%" class="center"><strong>กำไร-ขาดทุน (บาท) </strong></td>
        </tr>
      </thead>
      <tbody>
        <?php
	$strnum=0;
	$strAmount=0;
	$strCost=0;
	$strExpend=0;
	while($row = mysql_fetch_array($objQuery))
		{
		$strnum++;
?>
        <tr>
          <td style="text-align: center;"><?php echo $strnum;?></td>
          <td class="center"><?php 
		  $year = $row["year1"]+543;
		  echo $year;
?></td>
          <td class="center"><?php echo number_format($row["amount"], 2,'.',',');?></td>
          <td class="center"><?php echo number_format($row["cost"], 2,'.',',');?></td>
          <td class="center"><?php echo number_format($row["expend"], 2,'.',',');?></td>
          <td class="center"><?php
		  $amount = $row["amount"];
		  $cost = $row["cost"];
		  $expend = $row["expend"];
		  $profit = ($amount-$cost)-$expend;
		   echo number_format($profit, 2,'.',',');?></td>
        </tr>
        <?php
		$strTotal=$strTotal+$profit;
		$strAmount=$strAmount+($row["amount"]);
		$strCost=$strCost+($row["cost"]);
		$strExpend=$strExpend+($row["expend"]);
		}

	if($strnum>0) {
		?>
        <tr>
          <td colspan="2" style="text-align: center;"><strong>รวมสุทธิ (บาท) </strong></td>
          <td class="center"><strong><?php echo number_format($strAmount, 2,'.',',');?></strong></td>
          <td class="center"><strong><?php echo number_format($strCost, 2,'.',',');?></strong></td>
          <td class="center"><strong><?php echo number_format($strExpend, 2,'.',',');?></strong></td>
          <td class="center"><strong><?php echo number_format($strTotal, 2,'.',',');?></strong></td>
        </tr>
        <?php
		}
		if($strnum==0) {
?>
        <tr>
          <td colspan="9" class="center"><strong>ไม่มีรายการ</strong></td>
        </tr>
        <?php
		}
?>
      </tbody>
    </table>
    <?php } ?>
  </div>
</div>

</body>
</html>
